package com.bdqn.zhang.controller;

import com.bdqn.zhang.dto.CatDto;
import com.bdqn.zhang.dto.CatProjection;
import com.bdqn.zhang.pojo.Cat;
import com.bdqn.zhang.service.CatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.*;

/**
 * <Description> <br>
 *
 * @author tao<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2019/10/17 15:09 <br>
 * @see com.bdqn.zhang.controller <br>
 */
@RestController
public class CatController {
    @Autowired
    private CatService service;
//默认静态资源目录:http://localhost:8080/springbootjpasimple.sql
    /** 测试项目环境是否正常
     * http://localhost:8080/index
     * @return
     */
    @RequestMapping("/index")
    public Cat index() {
        Cat cat = new Cat(null,"张三", "男", 18, new Date(), Timestamp.valueOf("2019-10-16 11:49:45"),"");
        return cat;
    }
    /** 新增
     * http://localhost:8080/insertOne
     * @return
     */
    @RequestMapping("/insertOne")
    public Cat add() {
        Cat cat = new Cat(null,"张三", "男", 18, new Date(),Timestamp.valueOf("2019-10-16 11:49:45"),"");
        return service.insert(cat);
    }
    /** 排序
     * http://localhost:8080/orderBy
     * @return
     */
    @RequestMapping("/orderBy")
    public List<Cat> orderBy() {
        return service.queryAllSort("age");
    }

    /** 条件模糊查询:[JPA关键字形式]
     * http://localhost:8080/nameLike?name=m
     * http://localhost:8080/nameLike?name=七
     * @return
     */
    @RequestMapping("/nameLike")
    public List<Cat> nameLike(String name) {
        return service.queryByNameLike(name);
    }
    /** 条件模糊查询:[HQL形式]
     * http://localhost:8080/queryName?name=m
     * @return
     */
    @RequestMapping("/queryName")
    public List<Cat> queryName(String name) {
        return service.queryByName(name);
    }
    /** 条件模糊查询:[原生SQL形式]
     * http://localhost:8080/queryNameSQL?name=m
     * @return
     */
    @RequestMapping("/queryNameSQL")
    public List<Cat> queryNameSQL(String name) {
        return service.queryByNameSQL(name);
    }
    /**
     * http://localhost:8080/queryMaxAge
     * @return
     */
    @RequestMapping("/queryMaxAge")
    public Long queryMaxAge() {
        return service.queryMaxAge();
    }
    /**分页查询
     * http://localhost:8080/queryPage
     * @return
     */
    @RequestMapping("/queryPage")
    public Object queryPage() {
        Page<Cat> page = null;
        page = service.queryAllPage(0, 3);//第2页,每页3条;第几页从零开始,每页显示几条.
        System.out.println("queryPage page=>"+page);
        Long total = page.getTotalElements();
        List<Cat> list = page.getContent();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }
    /**带条件的排序查询
     * http://localhost:8080/nameLikeorderBy?name=m
     * @return
     */
    @RequestMapping("/nameLikeorderBy")
    public List<Cat> nameLikeorderBy(String name) {
        return service.queryNameLikeAllSort(name,"age");
    }
    /**带条件的分页查询
     * http://localhost:8080/queryNameLikePage?name=m
     * @return
     */
    @RequestMapping("/queryNameLikePage")
    public Object queryNameLikePage(String name) {
        Page<Cat> page = null;
        page = service.queryNameLikeAllPage(name, 0, 3);//第1页,每页3条;第几页从零开始,每页显示几条.
                System.out.println("page=>"+page);
        Long total = page.getTotalElements();
        List<Cat> list = page.getContent();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    /**带条件的投影查询
     * http://localhost:8080/findCatNameAndBirthdayById?id=m
     * @return
     */
    @RequestMapping("/findCatNameAndBirthdayById")
    public Map<String,Object> findCatNameAndBirthdayById(Integer id) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Collection<CatProjection> cats = service.findCatNameAndBirthdayById(id);
        for(CatProjection c:cats){
            map.put("Name:",c.getName());
            map.put("Brthday:",c.getBrthday());
        }
        return map;
    }

    /**带条件的投影查询
     * http://localhost:8080/findCatNameAndBirthdayById2?id=m
     * @return
     */
    @RequestMapping("/findCatNameAndBirthdayById2")
    public Map<String,Object> findCatNameAndBirthdayById2(Integer id) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Collection<CatDto> cats = service.findCatNameAndBirthdayById2(id);
        for(CatDto c:cats){
            map.put("Name:",c.getName());
            map.put("Brthday:",c.getBirthday());
        }
        return map;
    }
}
